<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="loadListData">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd" >新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="onSale" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
                <!--下架-->
                <el-form-item>
                    <el-button type="warning" @click="offSale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>

            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="pets" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="宠物名字" >
            </el-table-column>
            <el-table-column prop="saleprice" label="售价" >
            </el-table-column>
            <el-table-column prop="costprice" label="原价" >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" >
            </el-table-column>
            <el-table-column prop="state" label="状态" >
                <template scope="scope">
                    <span style="color: #11b95c" v-if="scope.row.state===1">待领养</span>
                    <span style="color: red" v-else-if="scope.row.state===0">已领养</span>
                    <span style="color: orange" v-else-if="scope.row.state===2">待处理</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="宠物名字" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="原价">
                    <el-input v-model="addForm.costprice"></el-input>
                </el-form-item>
                <el-form-item label="折扣价">
                    <el-input v-model="addForm.saleprice"></el-input>
                </el-form-item>
                <el-form-item label="产品图片">
                    <el-upload
                            class="upload-demo"
                            action="/api/fastDfs/upload"
                            :on-remove="handleRemove"
                            :on-success="uploadSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="产品详情">
                    <quill-editor  v-model="addForm.detail.intro" :options="quillConfig"></quill-editor>
                </el-form-item>
                <el-form-item label="预约须知">
                    <quill-editor  v-model="addForm.detail.adoptNotice" :options="quillConfig"></quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';


    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'

    import { quillEditor } from 'vue-quill-editor'
    //引入 配置文件js
    import quillConfig from '../../common/js/quill-config'


    export default {
        components:{
            quillEditor
        },
        data() {
            return {
                quillConfig:quillConfig,

                title:'新增',
                fileList:[],
                filters: {
                    name: ''
                },
                pets: [],
                total: 0,
                currentPage: 1,
                listLoading: false,
                sels: [],//列表选中列
                pageSize:5,

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ]
                },

                //新增界面数据
                addForm: {
                    id:null,
                    name:'',
                    costprice:0,
                    saleprice:0,
                    resources:0,
                    detail:{
                        id:null,
                        intro:'',
                        adoptNotice:''
                    }

                }
            }
        },


        methods: {
            //点击删除图片 执行的逻辑
            handleRemove(file, fileList) {
                let res = '';
                //拼接上传结果
                for(var i=0;i<fileList.length;i++ ){
                    if(i!=fileList.length-1){
                        res+=fileList[i].response.resultObj+",";
                    }else {
                        res+=fileList[i].response.resultObj;
                    }
                }
                this.addForm.resources=res;
            },

            //上传成功的处理方案
            uploadSuccess(response, file, fileList){
                //把响应的地址赋值给 shop对象里面的logo
                //this.addForm.resources=response.resultObj;
                //多次上传的结果进行拼接 AA.jpg,bb.jpg,pp.jpg
                //console.debug(fileList)
                let res = '';
                //拼接上传结果
                for(var i=0;i<fileList.length;i++ ){
                    if(i!=fileList.length-1){
                        res+=fileList[i].response.resultObj+",";
                    }else {
                        res+=fileList[i].response.resultObj;
                    }
                }
                this.addForm.resources=res;
            },

            // 分页页码数据
            handleCurrentChange(val) {
                this.currentPage = val;
                this.loadListData();
            },

            //获取用户列表
            loadListData() {
                let param = {
                    currentPage:this.currentPage,
                    pageSize:5,
                    keyword:this.filters.name
                }
                this.$http.post("/pet",param).then(res=>{
                    //res  请求返回的数据
                    this.pets=res.data.rows;
                    this.total=res.data.total;
                })
            },

            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete("/pet/"+row.id).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        let {success,message} =res.data;
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: '网络异常!!',
                                type: 'error'
                            });
                        }
                        this.loadListData();
                    });
                }).catch(() => {});
            },

            //显示编辑界面
            handleEdit: function (index, row) {
                this.fileList=[];//清空图片显示列表
                this.title="编辑";
                this.addFormVisible = true;
                this.addForm = Object.assign({}, row);
                console.log(this.addForm);
                //回显图片 本质--给 fileList[{name:xxx ,url:sss}] 赋值
                if(row.resources){
                    //分隔 resources
                    let arrs =row.resources.split(",");
                    for(var i=0;i<arrs.length;i++){
                        this.fileList.push({
                            name:'产品.jpg',
                            url:'http://121.37.194.36'+arrs[i]
                        })
                    }
                }
            },

            //显示新增界面
            handleAdd: function () {
                this.fileList=[];//清空图片列表
                this.title="新增";
                this.addFormVisible = true;
                // this.addForm = Object.assign({}, {});
                this.addForm= {
                    id: null,
                    name: '',
                    costprice: 0,
                    saleprice: 0,
                    resources: '',
                    detail: {
                        id: null,
                        intro: '',
                        adoptNotice: ''
                    }
                }
                this.addForm.state=0;//对状态特殊处理 默认就是下架
            },

            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);

                            this.$http.put("/pet",para).then((res) => {
                                this.addLoading = false;
                                let {success,message}=res.data;
                                if(success){
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.addFormVisible = false;
                                this.loadListData();
                                this.fileList=[];
                            });
                        });
                    }
                });
            },

            // 批量 多选框
            selsChange: function (sels) {
                this.sels = sels;
            },

            // 上架
            onSale(){
                // 哪些数据要上架  id
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认上架选中记录吗？', '提示', {
                    type: 'info'
                }).then(()=>{
                    this.listLoading = true;
                    //NProgress.start();
                    //let para = { ids: ids };
                    this.$http.patch("/pet/onSale",ids).then(res=>{
                        this.listLoading = false;
                        this.$message({
                            message: '上架成功',
                            type: 'success'
                        });
                        this.loadListData()
                    })
                })
            },

            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {
                });
            },

            // 下架
            offSale(){
                // 获取下架 id
                var ids = this.sels.map(item => item.id);
                // 提示框是否要下架
                this.$confirm('确认上架选中记录吗？', '提示', {
                    type: 'warning'
                }).then(()=>{
                    this.listLoading = true;
                    // 直接将要下线的ids传入后端，后端进行状态修改
                    this.$http.patch("/pet/offSale",ids).then(res=>{
                        this.listLoading = false;
                        this.$message({
                            message: '下架成功',
                            type: 'success'
                        });
                        this.loadListData()
                    })
                })
            }

        },
        mounted() {
            this.loadListData();
        }
    }

</script>

<style scoped>

</style>
